package com.party.course.util;

import java.time.DayOfWeek;
import java.time.LocalDate;


/**
 * 时间格式化工具类
 *
 * @author songkaiyue
 * @date 2022-06-07
 */
public class DateTimeUtill {
    public static void main(String[] args) {
        String time = "2025-05-08 10:41:01";
        String[] parts = time.split(" ");
        String date = parts[0];
        String timePart = parts[1];

        // 转换日期为星期几
        String weekDay = getWeek(date);
        // 转换时间为 12 小时制格式
        String formattedTime = formatTime(timePart);

        // 输出结果
        System.out.println("日期是：" + weekDay);
        System.out.println("时间是：" + formattedTime);
    }


    /**
     * 将时间格式化为12小时制
     * @param time
     * @return
     */
    public static String formatTime(String time) {
        String[] parts = time.split(":");
        int hour = Integer.parseInt(parts[0]);
        int minute = Integer.parseInt(parts[1]);
        if (hour > 12) {
            hour -= 12;
        }
        return String.format("%d:%02d", hour, minute);
    }

    /**
     * 根据时间获取上午还是下午时间
     * @param time
     * @return
     */
    public static String getToTime(String time){
        String[] timeParts = time.split(":");
        int hour = Integer.parseInt(timeParts[0]);
        String period = hour >= 12 ? "下午" : "上午";
        if (hour > 12) {
            hour -= 12;
        }
        String formattedTime = String.format("%s %02d:%s", period, hour, timeParts[1]);
//        System.out.println(formattedTime);
        return formattedTime;
    }

    /**
     * 根据日期获取星期几
     * @param date
     * @return
     */
    public static String getWeek (String date){
//        date = "2025-06-07";
        // 根据时间获取当天是周几
        LocalDate localDate = LocalDate.parse(date);
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        String chineseDayOfWeek = "星期" + numberToChinese(dayOfWeek.getValue());
//        System.out.println("当天是：" + chineseDayOfWeek);
        return chineseDayOfWeek;
    }

    /**
     * 将阿拉伯数字转换为中文数字的方法
     * @param number
     * @return
     */
    public static String numberToChinese(int number) {
        String[] chineseNumbers = {"零", "一", "二", "三", "四", "五", "六", "日", "八", "九"};
        return chineseNumbers[number];
    }
}
